package com.example.jiubodou.controller;

import com.example.jiubodou.domain.Student;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @ClassName ValidationController
 * @Description
 * @Author huyingliang
 * @Date 2024-05-06 14:38
 */
@RequestMapping("/validation")
@RestController
public class ValidationController {

    @PostMapping("/test")
    public String test(@RequestBody Student student) {
        System.out.println(student);
        return "test";
    }

    @PostMapping("/test2")
    public String test2(@RequestBody Student student) {
        if (student == null) {
            throw new RuntimeException("student is null");
        }
        if (null == student.getAge() ||
                !StringUtils.hasText(student.getName()) ||
                !StringUtils.hasText(student.getMobile())) {
            throw new RuntimeException("parmeter has null value");
        }
        if (student.getAge() < 0) {
            throw new RuntimeException("age is less than 0");
        }
        if (student.getMobile().length() != 11) {
            throw new RuntimeException("mobile length is not 11");
        }
        System.out.println(student);
        return "test";
    }

    @PostMapping("/test3")
    public String test3(@RequestBody @Valid Student student) {
        System.out.println(student);
        return "test";
    }


}
